package com.everyline.baselibrary.utils

import android.content.Context
import android.net.Uri
import android.widget.ImageView
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.io.File

/**
 *creatd by 天天挂线
 *email:746608431@qq.com
 *on 2020/7/9
 *描述:TODO
 */
class ImageLoaderHelper {
    companion object {
        @Volatile
        private var instance: ImageLoaderHelper? = null


        fun getInstance(): ImageLoaderHelper {
            return instance ?: synchronized(this) {
                instance ?: ImageLoaderHelper().also {
                    instance = it
                }
            }
        }
    }


    fun load(
        context: Context?,
        url: String?,
        iv: ImageView?
    ) {
        if (iv != null && context != null && url != null) {
            val options =
                RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL) //图片缓存
            Glide.with(context).load(url).apply(options).into(iv)
        }
    }

    fun load(
        context: Context?,
        url: String?,
        iv: ImageView?,
        with: Int,
        height: Int
    ) {
        if (iv != null && context != null && url != null) {
            val options =
                RequestOptions()
                    .override(with, height)
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL) //图片缓存
            Glide.with(context).load(url).apply(options).into(iv)
        }
    }

    fun loadCircleCrop(
        context: Context?,
        url: String?,
        iv: ImageView?
    ) {
        if (iv != null && context != null && url != null) {
            val options = RequestOptions.bitmapTransform(CircleCrop())
                .diskCacheStrategy(DiskCacheStrategy.ALL) //图片缓存
            Glide.with(context).load(url).apply(options).into(iv)
        }
    }

    fun loadRadius(
        context: Context?,
        url: String?,
        iv: ImageView?,
        radius: Int
    ) {
        if (iv != null && context != null && url != null) {
            Glide.with(context).load(url)
                .apply(RequestOptions.bitmapTransform(RoundedCorners(radius)))
                .into(iv)
        }
    }

    fun load(
        context: Context?,
        url: Uri?,
        iv: ImageView?
    ) {
        if (iv != null) {
            val options =
                RequestOptions()
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
            Glide.with(context!!).load(url).apply(options).into(iv)
        }
    }


    fun load(
        context: Context?,
        fileName: File?,
        iv: ImageView?
    ) {
        if (iv != null) {
            val options =
                RequestOptions()
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
            Glide.with(context!!).load(fileName).apply(options).into(iv)
        }
    }

    /**
     * 加载资源图片
     *
     * @param context
     * @param resourceId
     * @param iv
     */
    fun load(
        context: Context?,
        resourceId: Int?,
        iv: ImageView?
    ) {
        if (iv != null) {
            val options =
                RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
            Glide.with(context!!).load(resourceId).apply(options).into(iv)
        }
    }

    /**
     * 用rxjava 实现 网络图片下载到本地
     *
     * @param context
     * @param imgPath 图片网络地址
     */
    private fun saveImageToGallery(
        context: Context,
        imgPath: String
    ) {
        Observable.create<Boolean> { emitter ->
            val futureTarget = Glide.with(context).load(imgPath)
                .downloadOnly(
                    Target.SIZE_ORIGINAL,
                    Target.SIZE_ORIGINAL
                )
            try {
                val fileImg = futureTarget.get()
                val file = context.externalCacheDir
                val fileName = "image"
                val appDir = File(file, fileName)
                if (!appDir.exists()) {
                    appDir.mkdir()
                }
                val imgName =
                    System.currentTimeMillis().toString() + ".jpg"
                val destFile = File(appDir, imgName)
                val result =
                    FileUtils.copy(fileImg, destFile)
                emitter.onNext(result)
            } catch (e: Exception) {
                emitter.onNext(false)
            }
        }.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { aBoolean ->
                if (aBoolean) {
                    LogUtils.d("destFile===", "图片保存成功")
                } else {
                    LogUtils.d("destFile===", "图片保存失败")
                }
            }
    }

}